@extends('backend.layouts.app')

@section('title', $title = !is_null($agent->id) ? '编辑网站' : '添加代理' )

@section('breadcrumb')
    <a href="">系统设置</a>
    <a href="">代理管理</a>
    <a href="">{{$title}}</a>
@endsection

@section('content')
    <div class="layui-main">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>{{ $title }}</legend>
        </fieldset>

        <form class="layui-form layui-form-pane" method="POST"
              action="{{ !is_null($agent->id) ? route('agent.update', $agent->id) : route('agent.store') }}">
            {{ csrf_field() }}
            <input type="hidden" name="_method" class="mini-hidden"
                   value="{{ !is_null($agent->id) ? 'PATCH' : 'POST' }}">


            <div class="layui-form-item">
                <label class="layui-form-label">代理名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入网站显示名称"
                           autocomplete="off" class="layui-input" value="{{ old('name',$agent->name) }}"
                           @if(!is_null($agent->id)) disabled @endif>
                </div>
                <div class="layui-form-mid layui-word-aux">请填写2到32位，必须完全是字母、数字</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">比率</label>
                <div class="layui-input-inline">
                    <input type="text" name="rate" required lay-verify="required" placeholder="请输入比率"
                           autocomplete="off" class="layui-input" value="{{ old('rate',$agent->rate) }}">
                </div>
                <div class="layui-form-mid layui-word-aux">此比率为分红比率，不超过1的小数，最多保留4位小数</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="是" @if($agent->status) checked @endif>
                    <input type="radio" name="status" value="0" title="否" @if(!$agent->status) checked @endif>
                </div>
            </div>
            @if(is_null($agent->id))
                <div class="layui-form-item">
                    <label class="layui-form-label">网站后台账户</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" required lay-verify="required" placeholder="请输入后台账户名"
                               autocomplete="off" class="layui-input" value="{{ old('username','') }}">
                    </div>
                    <div class="layui-form-mid layui-word-aux">请填写2到32位，字母、数字、破折号（ - ）以及下划线（ _ ）</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">网站后台密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="password" required lay-verify="required" placeholder="请输入后台密码"
                               autocomplete="off" class="layui-input"
                               value="">
                    </div>
                    <div class="layui-form-mid layui-word-aux">请填写2到32位，字母、数字、破折号（ - ）以及下划线（ _ ）</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">确认网站后台密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="password_confirmation" required lay-verify="required"
                               placeholder="确认密码"
                               autocomplete="off" class="layui-input"
                               value="">
                    </div>
                </div>
            @endif
            <div class="layui-form-item" pane="" style="padding-top: 50px">
                <label class="layui-form-label">平台</label>

                @foreach($plats as $key => $val)
                    <div class="layui-input-block">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="checkbox" name="plats[id][]" lay-skin="primary"
                                       value="{{ $val['id'] }}" lay-filter="plats_id"
                                       title="{{ $val['main_game_plat_name'] }}"
                                       data-id="plats_id_{{$val['id']}}"
                                       @if(in_array($val['main_game_plat_code'],array_pluck($agentPlats,'main_game_plat_code'))) checked="" @endif >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="plats[rate][]"
                                       placeholder="比率" id="plats_id_{{$val['id']}}"
                                       @if(in_array($val['main_game_plat_code'],array_pluck($agentPlats,'main_game_plat_code'))) value="{{
                                           array_first($agentPlats,function($value)use($val){
                                               if($value['main_game_plat_code']==$val['main_game_plat_code']){
                                               return $value;
                                               }
                                           })['pivot']['rate']
                                       }}"
                                       @else
                                       disabled
                                       @endif
                                       autocomplete="off" class="layui-input" value="">
                            </div>
                            <div class="layui-form-mid layui-word-aux">不超过系统成本的小数，最多保留4位小数</div>
                        </div>
                    </div>

                @endforeach

            </div>
            <div class="layui-form-item  layui-container" style="padding-top: 50px">
                {{--<div class="layui-input-block">--}}
                <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                {{--</div>--}}
            </div>
        </form>
    </div>
@endsection

@section('scripts')
    <script>
        layui.use('form', function () {
            var form = layui.form;

            form.on('checkbox(plats_id)', function (data) {
                if (data.elem.checked) {
                    document.getElementById(data.elem.getAttribute('data-id')).disabled = false;
                    document.getElementById(data.elem.getAttribute('data-id')).setAttribute('lay-verify', 'rate');
                } else {
                    document.getElementById(data.elem.getAttribute('data-id')).disabled = true;
                    document.getElementById(data.elem.getAttribute('data-id')).setAttribute('lay-verify', '');
                }

            });

            form.verify({
                rate: [
                    /^0\.\d{1,4}/
                    , '比率必须大于等于0保留两位小数，并且小于1，'
                ]
            });
        });

    </script>
@endsection